import {defineStore} from 'pinia'
import {ref} from 'vue'
export const useUserStore = defineStore("user",()=>{
	// state :userInfo，token，
	const userInfo = ref({})
	const token = ref('');

	// actions：login，logout
	function login(data){
		var that = this;
		uni.request({
			url:"http://dida100.com:8888/api/login",
			method:"POST",
			data,
			success(res){
				console.log("res",res)
				if(res.data.code===200){
					// 更新store
					userInfo.value = res.data.user;
					token.value= res.data.token;
					// 本地存储
					uni.setStorageSync("userInfo",res.data.user)
					uni.setStorageSync("token",res.data.token);
					uni.showToast({
						title:res.data.msg,
						icon:'success'
					})
					// 跳转到首页
					uni.redirectTo({
						url:'/pages/index/home'
					})
				}else{
					uni.showToast({
						title:res.data.msg,
						icon:'none'
					})
				}
			},
			fail(){
				uni.showToast({
					title:"登录失败",
					icon:'error'
				})
			}
		})
	}
	function logout(){
		
	}
	return  {userInfo,token,login,logout}
})
